
package com.huaweicloud.saaslanding.install.applicationservice.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaweicloud.saaslanding.dao.common.entity.deployedresource.CseResource;
import com.huaweicloud.saaslanding.dao.common.entity.deployedresource.CseResourceRelation;
import com.huaweicloud.saaslanding.dao.common.repository.app.AppTemplateBaseMapper;
import com.huaweicloud.saaslanding.dao.common.repository.deployedresource.CseResourceBaseMapper;
import com.huaweicloud.saaslanding.dao.common.repository.deployedresource.CseResourceRelationBaseMapper;
import com.huaweicloud.saaslanding.dao.common.repository.tenant.TenantBaseMapper;
import com.huaweicloud.saaslanding.install.applicationservice.CseResourceService;
import com.huaweicloud.saaslanding.install.dto.ServiceManageFrameworkDto;

import lombok.extern.log4j.Log4j2;

import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;

/**
 * cse配置
 *
 * @since 2022-11-01
 */
@Service
@Log4j2
public class CseResourceServiceImpl extends ServiceImpl<CseResourceBaseMapper, CseResource>
    implements CseResourceService {

    @Resource
    private AppTemplateBaseMapper appTemplateBaseMapper;

    @Resource
    private TenantBaseMapper tenantBaseMapper;

    @Resource
    private CseResourceRelationBaseMapper cseResourceRelationBaseMapper;

    @Override
    public void saveCseResource(ServiceManageFrameworkDto serviceManageFrameworkDto, String kvIdBasic,
        String grayRouteKvId, String dynamicRouteConfigId, String dynamicRouteLinkDataId, String type) {

        // 资源表
        CseResource cseResource = new CseResource();
        cseResource.setAppName(serviceManageFrameworkDto.getAppName())
            .setServiceName(serviceManageFrameworkDto.getServiceName())
            .setComponentId(serviceManageFrameworkDto.getComponentId())
            .setBasicKvId(kvIdBasic)
            .setGrayRouteKvId(grayRouteKvId)
            .setDynamicRouteKvId(dynamicRouteConfigId)
            .setDynamicRouteLinkKvId(dynamicRouteLinkDataId)
            .setNamespaceId(serviceManageFrameworkDto.getNamespaceId())
            .setType(type);
        save(cseResource);
        Long cseResourceId = cseResource.getId();

        // CSE资源关系表
        Long appId = appTemplateBaseMapper.selectIdByType(1);
        String tenantCode = tenantBaseMapper.getIsvTenantCode();
        CseResourceRelation cseResourceRelation = new CseResourceRelation();
        cseResourceRelation.setAppId(appId)
            .setTenantCode(tenantCode)
            .setComponentId(serviceManageFrameworkDto.getComponentId())
            .setCseResourceId(cseResourceId);
        cseResourceRelationBaseMapper.insert(cseResourceRelation);
    }

}
